<template>
  <!-- 登入人既录入人 - 行政复议 -->
  <iframe v-if="iframeUrl" id="acceptIframe" :src="iframeUrl" width="100%" height="850vh" scrolling="yes" frameborder="0" />
</template>

<script>
import { SFNET_APPKEY, SFNET_SECRETKEY } from '@/store/mutation-types'
import storage from 'store'
import { APP_USER, ACCESS_TOKEN } from '@/store/mutation-types.js'
import { queryTaskidByApplyid } from '@/api/apply/apl_main.js'
const SFNET_URL = process.env.VUE_APP_SFNET_URL
export default {
  data() {
    return {
      isQuery: false, // 是否正在查询
      applyid: this.$route.params.applyid,
      matid: this.$route.params.matid,
      pattern: this.$route.params.pattern,
      sfnet_appKey: SFNET_APPKEY,
      sfnet_secretKey: SFNET_SECRETKEY,
      currentUser: storage.get(APP_USER), // 登录人信息
      iframeUrl: null
    }
  },
  created() {


    const token = storage.get(ACCESS_TOKEN)
    if (this.applyid && this.pattern && this.matid) {
      this.iframeUrl = `${SFNET_URL}/approveApplyForm/${this.applyid}/${this.matid}/${this.pattern}/${this.currentUser.userId}/${SFNET_APPKEY}/${SFNET_SECRETKEY}/${token}`
    } else {
      // 新增路由直接跳转，路由无法直接传参，为空则表示为新增的案件
      this.iframeUrl = `${SFNET_URL}/approveApplyForm/add/100000_360100-000201009000-QT-002-02/3/${this.currentUser.userId}/${SFNET_APPKEY}/${SFNET_SECRETKEY}/${token}`
    }
    console.log('iframeUrl', this.iframeUrl)
    // console.log('currentUser', this.currentUser)

    try{
        if(this.$route.query.url == '/AppeptExamineCommonDraft'){
          localStorage.setItem('AppeptExamineCommonDraft',JSON.stringify(this.$route.query.queryParam))
          localStorage.setItem('AppeptExamineCommonDraftUrl',JSON.stringify(this.$route.query.url))
          console.log('this.$route.query.AppeptExamineCommonDraft',this.$route.query.queryParam);
        }else if(this.$route.query.url == '/AppeptExamineDraft'){
          localStorage.setItem('AppeptExamineDraft',JSON.stringify(this.$route.query.queryParam))
          console.log('this.$route.query.AppeptExamineDraft',this.$route.query.queryParam);
        }else if(this.$route.query.url == '/AppeptExamineHandle'){
          localStorage.setItem('AppeptExamineHandle',JSON.stringify(this.$route.query.queryParam))
          console.log('this.$route.query.AppeptExamineHandle',this.$route.query.queryParam);
        }
    }catch{
      console.log('路由传参非json格式')
    }
  },
  mounted() {
    // 补正操作进来, 调用企业端方法设置补正状态
    if (this.$route.name == 'AppeptExamineAppendEdit') {
      setTimeout(() => {
        const iframeWindow = window.frames[0]
        iframeWindow.postMessage(
          {
            operation: 'setAppend'
          },
          '*'
        )
      }, 5000)
    }

    // 监听iframe页面点击按钮触发事件
    window.addEventListener('message', this.eventfinishAppend)
  },
  beforeDestroy() {
    window.removeEventListener('message', this.eventfinishAppend)
  },
  methods: {
    // 监听企业端补正完成事件
    eventfinishAppend(event) {
      if (event.data.operation == 'finishAppend') {
        this.finishAppend(event.data)
      }
    },
    // 企业端监听完成补正操作回调
    async finishAppend(record) {
      if (!record.applyid || !record.result) {
        this.$message.error('补正回调参数缺失')
      }
      let queryParam = {
        toTabKey: '4'
      }
      switch (record.result) {
        case 'Accpet': {
          queryParam.toCaseTabKey = 'accept_accept'
          break
        }
        case 'NoAccpet': {
          queryParam.toCaseTabKey = 'accept_noaccept'
          break
        }
        default: {
          this.$message.error('申报端返回未知的结果')
          return
        }
      }
      // 1. 查询流程的taskid
      const { data } = await queryTaskidByApplyid(record.applyid)
      // console.log('查询流程的taskid', data)
      if (null == data) {
        this.$message.error('业务任务信息查询失败')
      }
      this.$router.push({
        path: `/AppeptExamineHandleOperation/${data}/3`,
        query: {
          url: '/AppeptExamineHandle',
          queryParam: queryParam
        }
      })
    }
  }
}
</script>

<style scoped></style>
